﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using System.IO;
namespace syc.Sale
{
    public partial class FuLiaoSupplierInitialAudit : System.Web.UI.Page
    {
        protected HSSFWorkbook hssfworkbook;
        protected void Page_Load(object sender, EventArgs e)
        {
            int BillID = 0;
            if (!this.IsPostBack)
            {
                this.Danwei.DataBind();
                this.DZList.DataBind();
                this.Material.DataBind();
                this.LinkManList.DataBind();
                this.PhoneList.DataBind();
                Danwei_SelectedIndexChanged(null, null);

                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out BillID);
                }
                if (BillID > 0)
                {
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText = "SELECT [辅料供方初评表].[ID]  ,[供货商ID]  ,[产品ID] ,[单位概况],[质量体系情况] ,[评价意见] ,[评价人] ,[评价日期] ,[机动处] ,[生产安环处] ,[技术质量处] ,[生产车间] ,[批准人],辅料供货单位.地址,辅料供货单位.电话,辅料供货单位.联系人 FROM [SCM].[dbo].[辅料供方初评表] inner join 辅料供货单位 on [辅料供方初评表].供货商ID=辅料供货单位.ID where 辅料供方初评表.[ID]=" + BillID;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.ViewState["BillID"] = BillID;
                        this.Danwei.SelectedValue = Reader.GetInt32(1).ToString();
                        this.Material.SelectedValue = Reader.GetInt32(2).ToString();
                        this.Dwgk.Text = Reader.GetString(3).Trim();
                        this.Zltxqk.Text = Reader.GetString(4).Trim();
                        this.Pjyj.Text = Reader.GetString(5).Trim();
                        this.Pjr.Text = Reader.GetString(6).Trim();
                        this.Date.Text = Reader.GetDateTime(7).ToString("yyyy年M月d日");
                        this.Jdc.Text = Reader.GetString(8).Trim();
                        this.Scahc.Text = Reader.GetString(9).Trim();
                        this.Jszlc.Text = Reader.GetString(10).Trim();
                        this.Sccj.Text = Reader.GetString(11).Trim();
                        this.Pzr.Text = Reader.GetString(12).Trim();
                        try
                        {
                            this.Address.Text = Reader.GetString(13).Trim();
                        }
                        catch
                        {
                            this.Address.Text = "";
                        }
                        try
                        {
                            this.Phone.Text = Reader.GetString(14).Trim();
                        }
                        catch
                        {
                            this.Phone.Text = "";
                        }
                        try
                        {
                            this.LinkMan.Text = Reader.GetString(15).Trim();
                        }
                        catch
                        {
                            this.LinkMan.Text = "";
                        }

                        Reader.Close();
                    }


                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                }
            }
        }



        protected void Save_Click(object sender, EventArgs e)
        {

            DateTime dt;
            if (!DateTime.TryParse(this.Date.Text, out dt))
            {
                this.Message.Text = "·日期输入有误";
                return;
            }
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlTransaction Tran = Conn.BeginTransaction();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.Transaction = Tran;
            try
            {
                Cmd.CommandText = "FuLiao_Supplier_Audit";
                Cmd.CommandType = CommandType.StoredProcedure;
                if (this.ViewState["BillID"] == null)
                {
                    this.ViewState["BillID"] = -1;
                }
                Cmd.Parameters.Add(new SqlParameter("@供货商ID ", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(this.Danwei.Text)));
                Cmd.Parameters.Add(new SqlParameter("@产品ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(this.Material.Text)));
                Cmd.Parameters.Add(new SqlParameter("@单位概况", SqlDbType.NVarChar, 150, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.Dwgk.Text));
                Cmd.Parameters.Add(new SqlParameter("@质量体系情况", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.Zltxqk.Text));
                Cmd.Parameters.Add(new SqlParameter("@评价意见", SqlDbType.NVarChar, 150, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.Pjyj.Text));
                Cmd.Parameters.Add(new SqlParameter("@评价人", SqlDbType.NVarChar, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.Pjr.Text));
                Cmd.Parameters.Add(new SqlParameter("@评价日期", SqlDbType.Date, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, DateTime.Parse(this.Date.Text)));
                Cmd.Parameters.Add(new SqlParameter("@机动处", SqlDbType.NVarChar, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.Jdc.Text));
                Cmd.Parameters.Add(new SqlParameter("@生产安环处", SqlDbType.NVarChar, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.Scahc.Text));
                Cmd.Parameters.Add(new SqlParameter("@技术质量处", SqlDbType.NVarChar, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.Jszlc.Text));
                Cmd.Parameters.Add(new SqlParameter("@生产车间", SqlDbType.NVarChar, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.Sccj.Text));
                Cmd.Parameters.Add(new SqlParameter("@批准人", SqlDbType.NVarChar, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.Pzr.Text));
                Cmd.Parameters.Add(new SqlParameter("@BillID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.ViewState["BillID"]));
                Cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));

                Cmd.ExecuteNonQuery();

                this.ViewState["BillID"] = (int)Cmd.Parameters["@ID"].Value;
                int BillID = (int)this.ViewState["BillID"];
                if (BillID > 0)
                {
                    this.Message.Text = "·保存成功";
                }
                //else
                //{
                //    this.Message.Text = "·合同编号已经存在";
                //}
                Tran.Commit();
            }
            catch (Exception err)
            {
                Tran.Rollback();
                this.Message.Text = err.Message;
            }
            Cmd.Dispose();
            Tran.Dispose();
            Conn.Close();
            Conn.Dispose();

        }



        protected void Modify_Click(object sender, EventArgs e)
        {

        }

        protected void Danwei_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id = this.Danwei.Text;
            this.Address.Text = this.DZList.Items.FindByValue(id).Text;
            this.Phone.Text = this.PhoneList.Items.FindByValue(id).Text;
            this.LinkMan.Text = this.LinkManList.Items.FindByValue(id).Text;
        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("FuLiaoSupplierInitialAudit.aspx");
        }

        protected void Audit_Search_Click(object sender, EventArgs e)
        {
            Response.Redirect("FuLiaoSupplierInitialAudit_Search.aspx");
        }
        protected void InitializeWorkbook()
        {

            FileStream file = new FileStream(HttpContext.Current.Server.MapPath("/Sale/模板/供方初评表.xls"), FileMode.Open, FileAccess.Read);

            hssfworkbook = new HSSFWorkbook(file);

            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "供方初评表";
            hssfworkbook.DocumentSummaryInformation = dsi;

            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "报表";
            si.Title = "供方初评表";
            hssfworkbook.SummaryInformation = si;
        }
        protected void WriteToFile()
        {
            DateTime d;
            d = DateTime.Today;
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=\"" + d.ToShortDateString() + ".xls\"");
            hssfworkbook.Write(Response.OutputStream);
        }
        protected void Button_To_Click(object sender, EventArgs e)
        {
            InitializeWorkbook();
            HSSFSheet sheet1 = hssfworkbook.GetSheet("供方初评表");
            sheet1.GetRow(2).GetCell(1).SetCellValue(this.Danwei.SelectedItem.Text);
            sheet1.GetRow(2).GetCell(3).SetCellValue(this.Address.Text);
            sheet1.GetRow(3).GetCell(1).SetCellValue(this.Material.SelectedItem.Text);
            sheet1.GetRow(3).GetCell(3).SetCellValue(this.Phone.Text);
            sheet1.GetRow(3).GetCell(5).SetCellValue(this.LinkMan.Text);
            sheet1.GetRow(4).GetCell(1).SetCellValue(this.Dwgk.Text);
            sheet1.GetRow(6).GetCell(2).SetCellValue(this.Zltxqk.Text);
            sheet1.GetRow(7).GetCell(1).SetCellValue(this.Pjyj.Text);
            sheet1.GetRow(8).GetCell(2).SetCellValue(this.Pjr.Text);
            sheet1.GetRow(8).GetCell(5).SetCellValue(this.Date.Text);
            sheet1.GetRow(10).GetCell(1).SetCellValue(this.Jdc.Text);
            sheet1.GetRow(10).GetCell(2).SetCellValue(this.Scahc.Text);
            sheet1.GetRow(10).GetCell(3).SetCellValue(this.Jszlc.Text);
            sheet1.GetRow(10).GetCell(4).SetCellValue(this.Sccj.Text);
            sheet1.GetRow(11).GetCell(5).SetCellValue(this.Pzr.Text);
            WriteToFile();
            Response.End();
        } 
    }
}
